package common.customer.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import common.customer.bean.Customer;
import common.customer.service.CustomerService;
import common.system.bean.City;
import common.system.service.CityService;
import common.util.CommonCode;

public class LoginAction extends ActionSupport {
	private CustomerService customerService;
	private CityService cityService;
	/**
	 * 自动生成的version di
	 */
	private static final long serialVersionUID = 1L;
	private String userName;
	// 用户密码
	private String passWord;
	// email地址
	private String email;
	// 城市id
	private int cityId;
	private String question;
	private String answer;
	private String commonIp;

	private String resultStr;
	private String resultCode;

	private String validationCode;

	// 登录
	public String login() {
		if (userName == null || passWord == null || "".equals(userName.trim())
				|| "".equals(passWord.trim())) {
			resultCode = "0";
			resultStr = "用户名或者密码为空!";
			return SUCCESS;
		}
		if (validationCode == null || "".equals(validationCode.trim())) {
			resultCode = "0";
			resultStr = "验证码不能为空!";
			return SUCCESS;
		}
		String validationCodeSess = ServletActionContext.getRequest()
				.getSession().getAttribute("validationCode") == null ? null
				: ServletActionContext.getRequest().getSession()
						.getAttribute("validationCode").toString().trim();
		if (!validationCode.equals(validationCodeSess)) {
			resultCode = "0";
			resultStr = "验证码数据错误!";
			return SUCCESS;
		}
		try {
			int exitsUser = this.customerService.countCustomerByName(userName);
			if(exitsUser < 1){
				resultCode = "0";
				resultStr = "系统没有此用户!";
				return SUCCESS;
			}
			
			Customer cust = this.customerService.getCustomerByNameAndPass(
					userName, passWord);
			if (cust == null) {
				resultCode = "0";
				resultStr = "用户密码错误!";
				return SUCCESS;
			} else {
				ServletActionContext.getRequest().getSession()
						.setAttribute("user", cust);
				resultCode = "1";
				resultStr = "登录成功!";
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultCode = "0";
			resultStr = "请求失败，请联系管理员!";
			return SUCCESS;
		}
	}

	// 注册用户
	public String registeUser() {
		commonIp = acquireIpAddr(ServletActionContext.getRequest());
		System.out.println("用户的IP：" + commonIp);
		if (cityId == 0) {
			resultStr = "城市不能为空！";
			resultCode = "0";
			return SUCCESS;
		}
		if (userName == null || passWord == null || "".equals(userName.trim())
				|| "".equals(passWord.trim())) {
			resultStr = "用户名或者密码不能为空！";
			resultCode = "0";
			return SUCCESS;
		}
		City city = this.cityService.getCityByID(cityId);
		if (city == null) {
			resultStr = "城市不能为空！";
			resultCode = "0";
			return SUCCESS;
		}
		if (validationCode == null || "".equals(validationCode.trim())) {
			resultCode = "0";
			resultStr = "验证码不能为空!";
			return SUCCESS;
		}
		String validationCodeSess = ServletActionContext.getRequest()
				.getSession().getAttribute("validationCode") == null ? null
				: ServletActionContext.getRequest().getSession()
						.getAttribute("validationCode").toString().trim();
		if (!validationCode.equals(validationCodeSess)) {
			resultCode = "0";
			resultStr = "验证码数据错误!";
			return SUCCESS;
		}
		int temp = this.customerService.countCustomerByName(userName);
		if (temp > 0) {
			resultCode = "0";
			resultStr = "添加失败，用户已经存在!";
			return SUCCESS;
		}
		Customer cust = new Customer();
		cust.setUserName(userName);
		cust.setPassWord(passWord);
		cust.setQuestion(question);
		cust.setAnswer(answer);
		cust.setCommonIp(commonIp);
		cust.setEmail(email);
		cust.setUserRole(CommonCode.CUSTOMER_ROLE_COMMON);
		cust.setUserState(CommonCode.CUSTOMER_STATE_ACTIVE);
		cust.setCity(city);
		boolean result = this.customerService.addCustomer(cust);
		if (result) {
			resultCode = "1";
			ServletActionContext.getRequest().getSession()
					.setAttribute("user", cust);
			return SUCCESS;
		} else {
			resultCode = "0";
			resultStr = "添加失败，请联系管理员!";
			return SUCCESS;
		}

	}

	private String acquireIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	// ===============getter and setter method=======================
	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getResultStr() {
		return resultStr;
	}

	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}

	public String getResultCode() {
		return resultCode;
	}

	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	public String getValidationCode() {
		return validationCode;
	}

	public void setValidationCode(String validationCode) {
		this.validationCode = validationCode;
	}

	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getCityId() {
		return cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String getCommonIp() {
		return commonIp;
	}

	public void setCommonIp(String commonIp) {
		this.commonIp = commonIp;
	}

}
